package com.fish.common.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;


/**
 * 
 * 类名: PageTag <br>
 * 创建日期: 2010-5-19 <br>
 * 创建人: HeYong <br>
 * 功能描述: 分页标签 <br>
 * 修改日期: 2010-06-03  <br>
 * 修改人: HeYong <br>
 * 修改备注: <br>
 * CopyRight(c) 2010 <br>
 */
public class PageTag extends TagSupport{	
	private static final long serialVersionUID = -6349651025340136295L;
	private String pageUrl ="";//页面Url地址
	private String pageParams = "" ;//页面Url参数
	private Pager pager;
	
	public int doEndTag() throws JspException {
		return super.doEndTag();
	}
	
	
	public void setPageContext(PageContext pageContext) {
		super.setPageContext(pageContext);
		request = (HttpServletRequest)pageContext.getRequest();
	}

	public Pager getPager() {
		return pager;
	}


	public int doStartTag() throws JspException {
		this.initTag(getPager(),request);	
		StringBuffer strTag = new StringBuffer();
		try {
			strTag.append("<table class='pages'><tr><td>");
			strTag.append("<form action='"+this.getPageUrl()+"' name=\"pagerFrom\" id=\"pagerFrom\" method=\"post\" >");
			strTag.append("共" + this.getTotalPage() + "页/"+this.getTotalRecords()+"条&nbsp;");
		
			if(this.getPageParams() != null){
				String[] params = this.getPageParams().split("&");
			      for (String param : params){
			        String[] p = param.split("=");
			        if (p.length == 2) {
			        	strTag.append("<input type='hidden' name='" + p[0] + "' value='" + p[1] + "' />");
			        }
			      }
			}
			strTag.append("每页<input type='text' id='pageSize' name='pageSize' size='1' class='size' maxlength=5 value='"+this.getPageSize()+"'/>条");
			strTag.append("转第<input type='text' id='page' name='page' size='1' class='size' maxlength=5 value='"+this.getCurPage()+"'/>");
			
			strTag.append("&nbsp;<a class=\"go\" href=\"javascript:void(0);\" onclick=\"document.getElementById('pagerFrom').submit();\">go</a>");
			strTag.append(this.getStrPager());
			strTag.append("</form></td></tr></table>");
			pageContext.getOut().write(strTag.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	
	public String getStrPager(){
		StringBuffer strb = new StringBuffer();
		String pageSizeString = "";
		if(this.getPageUrl().indexOf("?")!=-1){
			pageSizeString+="&";
		}else{
			pageSizeString+="?";
		}
		pageSizeString += "pageSize="+this.getPageSize()+"&count="+this.getTotalRecords();//定义每页显示的
		
		if(this.getCurPage() > 1){
			strb.append("<a href='").append(this.getPageUrl()+pageSizeString).append("&page=1");
			if(!"".equals(this.getPageParams())){
				strb.append("&"+this.getPageParams());
			}
			strb.append("'>首页</a>&nbsp;");			
		}else{
			strb.append("<a class='disabled'>首页</a>&nbsp;");
		}
		
		if(this.getCurPage() <= this.getTotalPage() && this.getCurPage() >1 ){
			if("".equals(this.getPageParams())){
				strb.append("<a href='"+this.getPageUrl()+pageSizeString+"&page="+((this.getCurPage())-1)+"'>上一页</a>&nbsp;");
			}else{
				strb.append("<a href='"+this.getPageUrl()+pageSizeString+"&page="+((this.getCurPage())-1)+"&"+this.getPageParams()+"'>上一页</a>&nbsp;");
			}
		}else{
			strb.append("<a class='disabled'>上一页</a>&nbsp;");
		}
		
		if(showNum == 1){
			int star = 1;
			int end = 1;
			if(totalPage<=9){
				star = 1;
				end = totalPage;
			}else if(curPage<=9){
				star = 1;
				end = 10;
			}else if(totalPage-curPage<9){
				end = totalPage;
				star = totalPage-10;
			}else{
				star = curPage-5;
				end = curPage+4;	
			}
			
			for(int i=star;i<=end;i++){
				strb.append("");
				if( i== this.getCurPage()){
					strb.append("<a><font color='red'>"+i+"</font></a>");
				}else{
					if("".equals(this.getPageParams())){
						strb.append("<a href='"+this.getPageUrl()+pageSizeString+"&page="+i+"'>"+i+"</a>");
					}else{
						strb.append("<a href='"+this.getPageUrl()+pageSizeString+"&page="+i+"&"+this.getPageParams()+"'>"+i+"</a>");
					}
				}
				strb.append("&nbsp;");
			}
		}
		
		if(this.getCurPage()< this.getTotalPage()){
			if("".equals(this.getPageParams())){
				strb.append("<a href='"+this.getPageUrl()+pageSizeString+"&page="+((this.getCurPage())+1)+"'>下一页</a>&nbsp;");
			}else{
				strb.append("<a href='"+this.getPageUrl()+pageSizeString+"&page="+((this.getCurPage())+1)+"&"+this.getPageParams()+"'>下一页</a>&nbsp;");
			}
		}else{
			strb.append("<a class='disabled'>下一页</a>&nbsp;");
		}
		
		if(this.curPage == this.getTotalPage() || this.getTotalRecords()==0){
			strb.append("<a class='disabled'>末页</a>&nbsp;");
		}else{
			if("".equals(this.getPageParams())){
				strb.append("<a href='"+this.getPageUrl()+pageSizeString+"&page="+this.getTotalPage()+"'>末页</a>&nbsp;");
			}else{
				strb.append("<a href='"+this.getPageUrl()+pageSizeString+"&page="+this.getTotalPage()+"&"+this.getPageParams()+"'>末页</a>&nbsp;");
			}
		}
		return strb.toString();
	}
	
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getCurPage() {
		return curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public int getTotalRecords() {
		return totalRecords;
	}

	public int getShowNum() {
		return showNum;
	}
	public void setShowNum(int showNum) {
		this.showNum = showNum;
	}
	public String getPageParams() {
		return pageParams;
	}
	
	/**
	 * 获取多参数
	 * 
	 * @return
	 */
	private  String getPageParams(HttpServletRequest request) {
		StringBuilder params = new StringBuilder();
		String parameters = "";
		if (request == null){
			return parameters;
		}
		Map<?, ?> parameterMap = request.getParameterMap();
		Iterator<?> it = parameterMap.keySet().iterator(); // 得到所有属性名称
		while (it.hasNext()) {
			String paramName = it.next().toString();
			if(!"specifiedCurrentPage".equals(paramName)){
			if (!isCheckStrExit(paramName, TEMPS)) {
				String[] values = request.getParameterValues(paramName);
				if(values!=null&&values.length>0){
					for(String value:values){
						params.append(paramName)
								.append("=")
								.append(value.trim())
								.append("&");
					}
				}
			}
			}
		}
		if (params.indexOf("&") >= 0) {
			params = params.deleteCharAt(params.lastIndexOf("&"));
		}
		parameters = params.toString();
		return parameters;
	}
	
	/** 初始化标签参数  */
	private void initTag(Pager pager,HttpServletRequest request){
		this.totalRecords = pager.getCount();
		this.totalPage = pager.getPageCount();
		this.pageSize = pager.getPageSize();
		this.curPage = pager.getPage();
		this.pageParams = getPageParams(request);
	}

	
	private int totalRecords = 0;//总记录数
	private int pageSize = 20 ;//一页显示记录数
	private int curPage = 0;//当前显示页
	private int totalPage = 0;//一共有多少页
	private int showNum = 0;//是否显示数据导航  0为不显示，1为显示  
	private HttpServletRequest request;
	
	private static final String[] TEMPS = { "page", "pageSize", "count","id"};

	public static boolean isCheckStrExit(String str,String []Arg){
		boolean isExist = false;
		if(Arg!=null && Arg.length>0){
			int len = Arg.length;
			String arg = "";
			for(int i=0;i<len;i++){
				arg = Arg[i].trim();
				if(str.equals(arg)){
					isExist = true;
					break;
				}
			}
		}
		return isExist;
	}
	
	

}
